﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XML
{
    public class PatientManagementAPI
    {
        private XmlDocument m_File = null;
        private String m_Path = null;

        public bool OpenXmlFile(String _Path)
        {
            if (m_File == null)
            {
                m_File = new XmlDocument();
            }
            try
            {
                m_File.Load(_Path);
                m_Path = _Path;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
            return true;
        }

        public bool CloseXmlFile()
        {
            m_File = null;
            m_Path = null;
            return true;
        }

        public bool CreateXmlFile(String _Path)
        {
            if (!System.IO.File.Exists(_Path))
            {
                System.IO.File.Create(_Path);
            }
            else
            {
                return false;
            }
            bool Result = OpenXmlFile(_Path);
            if (Result)
            {
                m_Path = _Path;
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool WriteXmlFile()
        {
            try
            {
                m_File.Save(m_Path);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
            return true;
        }


        public Patient OpenPatient(String _FirstName, String _Name)
        {
            _FirstName.Trim();
            _Name.Trim();

            if ((_FirstName == null || _FirstName.Equals("") && (_Name == null || _Name.Equals(""))))
            {
                return null;
            }
            
            
            int index = 0;
            XmlNode PatientNode = null;
            try
            {
                XmlNodeList PatientList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
                
                if (_FirstName == null || _FirstName.Equals(""))
                {
                    for (index = 0; index < PatientList.Count; ++index)
                    {
                        if (_Name.Equals(PatientList[index].ChildNodes[0].InnerText))
                        {
                            PatientNode = PatientList[index];
                            break;
                        }
                    }
                }
                else if (_Name == null || _Name.Equals(""))
                {
                    for (index = 0; index < PatientList.Count; ++index)
                    {
                        if (_FirstName.Equals(PatientList[index].ChildNodes[1].InnerText))
                        {
                            PatientNode = PatientList[index];
                            break;
                        }
                    }
                }
                else
                {
                    for (index = 0; index < PatientList.Count; ++index)
                    {
                        if ((_FirstName.Equals(PatientList[index].ChildNodes[0].InnerText)) && (_Name.Equals(PatientList[index].ChildNodes[1].InnerText)))
                        {
                            PatientNode = PatientList[index];
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            if(PatientNode == null)
            {
                return null;
            }
            else
            {
                return FillPatient(PatientNode);
            }
        }

        public Patient OpenPatient(int _ID)
        {
            XmlNodeList PatientList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
            XmlNode PatientNode = null;
            int index = 0;
            for (index = 0; index < PatientList.Count; ++index)
            {
                if (_ID == int.Parse(PatientList[index].Attributes["ID"].Value))
                {
                    PatientNode = PatientList[index];
                    break;
                }
            }

            if (PatientNode == null)
            {
                return null;
            }
            else
            {
                return FillPatient(PatientNode);
            }
        }

        private Patient FillPatient(XmlNode _PatientNode)
        {
            Patient P = new Patient();

            P.m_ID        = int.Parse(_PatientNode.Attributes["ID"].Value);
            P.m_Gender    = _PatientNode.Attributes["Geschlecht"].Value;
            P.m_Name      = _PatientNode.ChildNodes[0].InnerText;
            P.m_FirstName = _PatientNode.ChildNodes[1].InnerText;
            P.m_Title     = _PatientNode.ChildNodes[2].InnerText;
            P.m_Birthday  = StringToDate(_PatientNode.ChildNodes[3].InnerText);

            P.m_Adress.m_Street   = _PatientNode.ChildNodes[4].ChildNodes[0].InnerText;
            P.m_Adress.m_Number   = _PatientNode.ChildNodes[4].ChildNodes[1].InnerText;
            P.m_Adress.m_PostCode = _PatientNode.ChildNodes[4].ChildNodes[2].InnerText;
            P.m_Adress.m_Place    = _PatientNode.ChildNodes[4].ChildNodes[3].InnerText;

            P.m_Height     = float.Parse(_PatientNode.ChildNodes[5].InnerText);
            P.m_HeightUnit = _PatientNode.ChildNodes[5].Attributes["Masseinheit"].Value;
            P.m_Weigth     = float.Parse(_PatientNode.ChildNodes[6].InnerText);
            P.m_WeightUnit = _PatientNode.ChildNodes[6].Attributes["Masseinheit"].Value;
            
            P.m_Smoker.m_Typ   = _PatientNode.ChildNodes[7].Attributes["Rauchertyp"].Value;
            P.m_Smoker.m_Since = StringToDate(_PatientNode.ChildNodes[7].ChildNodes[0].InnerText);
            P.m_Smoker.m_Count = _PatientNode.ChildNodes[7].ChildNodes[1].InnerText;

            P.m_EMail       = _PatientNode.ChildNodes[8].InnerText;
            P.m_Telephone   = _PatientNode.ChildNodes[9].InnerText;
            P.m_MobilePhone = _PatientNode.ChildNodes[10].InnerText;

            P.m_Treatment.m_Type                = _PatientNode.ChildNodes[11].ChildNodes[0].InnerText;
            P.m_Treatment.m_Briefing.m_Goal     = _PatientNode.ChildNodes[11].ChildNodes[1].ChildNodes[0].InnerText;
            P.m_Treatment.m_Briefing.m_Comment  = _PatientNode.ChildNodes[11].ChildNodes[1].ChildNodes[1].InnerText;
            P.m_Treatment.m_Date                = StringToDate(_PatientNode.ChildNodes[11].ChildNodes[2].InnerText);
            P.m_Treatment.m_Procedure           = _PatientNode.ChildNodes[11].ChildNodes[3].InnerText;

            P.m_Payment.m_Paid          = "ja".Equals(_PatientNode.ChildNodes[12].Attributes["Bezahlt"].Value);
            P.m_Payment.m_PaimentType   = _PatientNode.ChildNodes[12].Attributes["Zahlungsart"].Value;
            P.m_Payment.m_Currency      = _PatientNode.ChildNodes[12].Attributes["Waehrung"].Value;
            P.m_Payment.m_Guaranty      = "ja".Equals(_PatientNode.ChildNodes[12].Attributes["Garantie"].Value);
            P.m_Payment.m_Price         = float.Parse(_PatientNode.ChildNodes[12].InnerText);

            return P;
        }

        private DateTime StringToDate(String _DateString)
        {
            String[] Split;
            DateTime dt;
            try
            {
                Split = _DateString.Split('.');
                dt = new DateTime(int.Parse(Split[2]),int.Parse(Split[1]),int.Parse(Split[0]));

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return new DateTime();
            }

            return dt;
        }

        public bool SavePatient(Patient P)
        {
            XmlNodeList PatientList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
            XmlNode _PatientNode = null;
            int index = 0;
            for (index = 0; index < PatientList.Count; ++index)
            {
                if (P.m_ID == int.Parse(PatientList[index].Attributes["ID"].Value))
                {
                    _PatientNode = PatientList[index];
                    break;
                }
            }

            if (_PatientNode == null)
            {
                return false;
            }
            else
            {
                _PatientNode.Attributes["Geschlecht"].Value = P.m_Gender;
                _PatientNode.ChildNodes[0].InnerText        = P.m_Name;
                _PatientNode.ChildNodes[1].InnerText        = P.m_FirstName;
                _PatientNode.ChildNodes[2].InnerText        = P.m_Title;
                _PatientNode.ChildNodes[3].InnerText        = P.m_Birthday.ToString("dd.MM.yyyy");

                _PatientNode.ChildNodes[4].ChildNodes[0].InnerText = P.m_Adress.m_Street;
                _PatientNode.ChildNodes[4].ChildNodes[1].InnerText = P.m_Adress.m_Number;
                _PatientNode.ChildNodes[4].ChildNodes[2].InnerText = P.m_Adress.m_PostCode;
                _PatientNode.ChildNodes[4].ChildNodes[3].InnerText = P.m_Adress.m_Place;

                _PatientNode.ChildNodes[5].InnerText                       = P.m_Height.ToString();
                _PatientNode.ChildNodes[5].Attributes["Masseinheit"].Value = P.m_HeightUnit;
                _PatientNode.ChildNodes[6].InnerText                       = P.m_Weigth.ToString();
                _PatientNode.ChildNodes[6].Attributes["Masseinheit"].Value = P.m_WeightUnit;

                _PatientNode.ChildNodes[7].Attributes["Rauchertyp"].Value = P.m_Smoker.m_Typ;
                _PatientNode.ChildNodes[7].ChildNodes[0].InnerText        = P.m_Smoker.m_Since.ToString("dd.MM.yyyy");
                _PatientNode.ChildNodes[7].ChildNodes[1].InnerText        = P.m_Smoker.m_Count;

                _PatientNode.ChildNodes[8].InnerText  = P.m_EMail;
                _PatientNode.ChildNodes[9].InnerText  = P.m_Telephone;
                _PatientNode.ChildNodes[10].InnerText = P.m_MobilePhone;

                _PatientNode.ChildNodes[11].ChildNodes[0].InnerText               = P.m_Treatment.m_Type;
                _PatientNode.ChildNodes[11].ChildNodes[1].ChildNodes[0].InnerText = P.m_Treatment.m_Briefing.m_Goal;
                _PatientNode.ChildNodes[11].ChildNodes[1].ChildNodes[1].InnerText = P.m_Treatment.m_Briefing.m_Comment;
                _PatientNode.ChildNodes[11].ChildNodes[2].InnerText               = P.m_Treatment.m_Date.ToString("dd.MM.yyyy");
                _PatientNode.ChildNodes[11].ChildNodes[3].InnerText               = P.m_Treatment.m_Procedure;

                if (P.m_Payment.m_Paid)
                {
                    _PatientNode.ChildNodes[12].Attributes["Bezahlt"].Value = "ja";
                }
                else
                {
                    _PatientNode.ChildNodes[12].Attributes["Bezahlt"].Value = "nein";
                }
                _PatientNode.ChildNodes[12].Attributes["Zahlungsart"].Value = P.m_Payment.m_PaimentType;
                _PatientNode.ChildNodes[12].Attributes["Waehrung"].Value    = P.m_Payment.m_Currency;
                if (P.m_Payment.m_Guaranty)
                {
                    _PatientNode.ChildNodes[12].Attributes["Garantie"].Value = "ja";
                }
                else
                {
                    _PatientNode.ChildNodes[12].Attributes["Garantie"].Value = "nein";
                }
                _PatientNode.ChildNodes[12].InnerText                       = P.m_Payment.m_Price.ToString();

                return WriteXmlFile();
            }
        }

        public List<PatientFullName> GetPatientlist()
        {
            List<PatientFullName> PatientList = new List<PatientFullName>();

            XmlNodeList PatientNodeList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
            for (int index = 0; index < PatientNodeList.Count; ++index)
            {
                PatientList.Add(new PatientFullName(int.Parse(PatientNodeList[index].Attributes["ID"].Value), PatientNodeList[index].ChildNodes[1].InnerText, PatientNodeList[index].ChildNodes[0].InnerText));
            }

            return PatientList;
        }

        public bool CreatePatient(Patient _Patient)
        {
            XmlNodeList PatientList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
            XmlNode NewPatient = PatientList[0].Clone();

            _Patient.m_ID = GetNewID();

            if (NewPatient == null)
            {
                return false;
            }
            else
            {
                NewPatient.Attributes["ID"].Value           = _Patient.m_ID.ToString();
                NewPatient.Attributes["Geschlecht"].Value   = _Patient.m_Gender;
                NewPatient.ChildNodes[0].InnerText          = _Patient.m_Name;
                NewPatient.ChildNodes[1].InnerText          = _Patient.m_FirstName;
                NewPatient.ChildNodes[2].InnerText          = _Patient.m_Title;
                NewPatient.ChildNodes[3].InnerText          = _Patient.m_Birthday.ToString("dd.MM.yyyy");

                NewPatient.ChildNodes[4].ChildNodes[0].InnerText = _Patient.m_Adress.m_Street;
                NewPatient.ChildNodes[4].ChildNodes[1].InnerText = _Patient.m_Adress.m_Number;
                NewPatient.ChildNodes[4].ChildNodes[2].InnerText = _Patient.m_Adress.m_PostCode;
                NewPatient.ChildNodes[4].ChildNodes[3].InnerText = _Patient.m_Adress.m_Place;

                NewPatient.ChildNodes[5].InnerText                       = _Patient.m_Height.ToString();
                NewPatient.ChildNodes[5].Attributes["Masseinheit"].Value = _Patient.m_HeightUnit;
                NewPatient.ChildNodes[6].InnerText                       = _Patient.m_Weigth.ToString();
                NewPatient.ChildNodes[6].Attributes["Masseinheit"].Value = _Patient.m_WeightUnit;

                NewPatient.ChildNodes[7].Attributes["Rauchertyp"].Value = _Patient.m_Smoker.m_Typ;
                NewPatient.ChildNodes[7].ChildNodes[0].InnerText        = _Patient.m_Smoker.m_Since.ToString("dd.MM.yyyy");
                NewPatient.ChildNodes[7].ChildNodes[1].InnerText        = _Patient.m_Smoker.m_Count;

                NewPatient.ChildNodes[8].InnerText  = _Patient.m_EMail;
                NewPatient.ChildNodes[9].InnerText  = _Patient.m_Telephone;
                NewPatient.ChildNodes[10].InnerText = _Patient.m_MobilePhone;

                NewPatient.ChildNodes[11].ChildNodes[0].InnerText               = _Patient.m_Treatment.m_Type;
                NewPatient.ChildNodes[11].ChildNodes[1].ChildNodes[0].InnerText = _Patient.m_Treatment.m_Briefing.m_Goal;
                NewPatient.ChildNodes[11].ChildNodes[1].ChildNodes[1].InnerText = _Patient.m_Treatment.m_Briefing.m_Comment;
                NewPatient.ChildNodes[11].ChildNodes[2].InnerText               = _Patient.m_Treatment.m_Date.ToString("dd.MM.yyyy");
                NewPatient.ChildNodes[11].ChildNodes[3].InnerText               = _Patient.m_Treatment.m_Procedure;

                if (_Patient.m_Payment.m_Paid)
                {
                    NewPatient.ChildNodes[12].Attributes["Bezahlt"].Value = "ja";
                }
                else
                {
                    NewPatient.ChildNodes[12].Attributes["Bezahlt"].Value = "nein";
                }
                NewPatient.ChildNodes[12].Attributes["Zahlungsart"].Value = _Patient.m_Payment.m_PaimentType;
                NewPatient.ChildNodes[12].Attributes["Waehrung"].Value    = _Patient.m_Payment.m_Currency;
                if (_Patient.m_Payment.m_Guaranty)
                {
                    NewPatient.ChildNodes[12].Attributes["Garantie"].Value = "ja";
                }
                else
                {
                    NewPatient.ChildNodes[12].Attributes["Garantie"].Value = "nein";
                }
                NewPatient.ChildNodes[12].InnerText                       = _Patient.m_Payment.m_Price.ToString();

                m_File.LastChild.AppendChild(NewPatient);

                return WriteXmlFile();
            }
        }

        private int GetNewID()
        {
            int HighestIndex = 0;
            XmlNodeList PatientList = m_File.SelectNodes("//Teilnehmerdaten/Teilnehmer");
            int index = 0;
            int ID = 0;
            for (index = 0; index < PatientList.Count; ++index)
            {
                ID = int.Parse(PatientList[index].Attributes["ID"].Value);
                if (ID > HighestIndex)
                {
                    HighestIndex = ID;
                }
            }
            return (ID + 1);
        }
    }
}
